package com.situ.mall.service.impl;

import java.util.List;

import javax.jws.soap.SOAPBinding.Use;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.mall.common.ServerResponse;
import com.situ.mall.entity.Category;
import com.situ.mall.mapper.CategoryMapper;
import com.situ.mall.service.ICategoryService;
import com.situ.mall.vo.CategoryCountVO;

@Service
public class CategoryServiceImpl implements ICategoryService {
	@Autowired
	private CategoryMapper categoryMapper;

	

	@Override
	public ServerResponse pageList(Integer page, Integer limit,Category category) {
		//分页插件
		PageHelper.startPage(page, limit);
		//查找数据
		List<Category> list = categoryMapper.pageList(category);
		//封装pageInfo
		PageInfo pageInfo = new PageInfo<>(list);
		//查找总数据
		Integer count = (int)pageInfo.getTotal();
		//返回
		return ServerResponse.createSuccess("查找成功", count, list);
	}
	
	//Id删除
	@Override
	public ServerResponse deleteById(Integer id) {
		try {
			Integer count = categoryMapper.deleteByPrimaryKey(id);
			if (count == 1) {
				return ServerResponse.createSuccess("删除成功");
			} else {
				return ServerResponse.createError("删除失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("删除失败");
		}
	}

	@Override
	public ServerResponse deleteAll(String ids) {
		String[] idArray = ids.split(",");
		try {
			Integer count = categoryMapper.deleteAll(idArray);
			if (count == idArray.length) {
				return ServerResponse.createSuccess("删除成功");
			} else {
				return ServerResponse.createError("删除失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("删除失败");
		}
	}

	@Override
	public ServerResponse selectTopCategory() {
		try {
			List<Category> list = categoryMapper.selectTopCategory();
			if (list == null) {
				return ServerResponse.createSuccess("查询商品分类信息失败");
			}
			return ServerResponse.createSuccess("查找成功", list);
		} catch (Exception e) {
			return ServerResponse.createSuccess("查询商品分类信息失败");
		}
	}

	@Override
	public ServerResponse selectSecondCategory(Integer topCategoryId) {
		try {
			List<Category> list = categoryMapper.selectSecondCategory(topCategoryId);
			if (list == null) {
				return ServerResponse.createSuccess("查询商品分类信息失败");
			}
			return ServerResponse.createSuccess("查找成功", list);
			
		} catch (Exception e) {
			return ServerResponse.createSuccess("查询商品分类信息失败");
		}
	}

	//添加一级分类
	@Override
	public ServerResponse insert(Category category) {
		try {
			Integer count = categoryMapper.insert(category);
			if (count == 1) {
				return ServerResponse.createSuccess("一级分类添加成功");
			} else {
				return ServerResponse.createError("一级分类添加失败");
			}
			
		} catch (Exception e) {
			return ServerResponse.createError("一级分类添加失败");
		}
	}

	@Override
	public ServerResponse insert2(Category category) {
		try {
			Integer count = categoryMapper.insert(category);
			if (count == 1) {
				return ServerResponse.createSuccess("二级分类添加成功");
			} else {
				return ServerResponse.createError("二级分类添加失败");
			}
			
		} catch (Exception e) {
			return ServerResponse.createError("二级分类添加失败");
		}
	}

	@Override
	public Category toUpdate(Integer id) {
		return categoryMapper.selectByPrimaryKey(id);
	}

	//修改一级分类
	@Override
	public ServerResponse update(Category category) {
		try {
			Integer count = categoryMapper.updateByPrimaryKeySelective(category);
			if (count == 1) {
				return ServerResponse.createSuccess("修改成功");
			} else {
				return ServerResponse.createError("修改失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("修改失败");
		}
	}

	//前台查询一级分类展示
	@Override
	public List<Category> selectTop() {
		return categoryMapper.selectTop();
	}
	
	//前台查询二级分类展示
	@Override
	public List<Category> selectsecond() {
		return categoryMapper.selectSecond();
	}

	//统计商品数量
	@Override
	public ServerResponse selectCategoryCount() {
		List<CategoryCountVO> list = categoryMapper.selectCategoryCount();
		return ServerResponse.createSuccess("成功", list);
	}
}
